package org.blim.blackdawn.easymock;

import static org.easymock.EasyMock.reportMatcher;

import org.easymock.IArgumentMatcher;

public class ArrayDeltaEquals implements IArgumentMatcher {
	
	private final float[] mExpected;
	private final float mDelta;

	public ArrayDeltaEquals(float[] expected, float delta) {
		mExpected = expected;
		mDelta = delta;
	}

	public boolean matches(Object actual) {
		if (!(actual instanceof float[])) {
			return false;
		}
		
		float[] actualArray = (float[]) actual;
		
		if (actualArray.length != mExpected.length) {
			return false;
		}
		
		for (int i = 0; i < mExpected.length; i++) {
			if (Math.abs(mExpected[i] - actualArray[i]) > mDelta) {
				return false;
			}
		}
		
		return true;
	}

	public void appendTo(StringBuffer buffer) {
	        buffer.append("aryEqDelta(");
	        buffer.append(mExpected);
	        buffer.append(", ");
	        buffer.append(mDelta);
	        buffer.append(")");
	}
	
	public static float[] aryEqDelta(float[] input, float delta) {
	    reportMatcher(new ArrayDeltaEquals(input, delta));
	    return input;
	}

}